package com.kth.subtranslator;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.border.BevelBorder;

import com.google.common.eventbus.EventBus;
import com.kth.subtranslator.ui.EventQueueProxy;
import com.kth.subtranslator.ui.SaveSubDialog.ISaveListener;
import com.kth.subtranslator.ui.controller.MenuBarController;
import com.kth.subtranslator.ui.controller.MenuItemsController;
import com.kth.subtranslator.ui.controller.SrtPanelController;
import com.kth.subtranslator.ui.controller.StatusBarController;
import com.kth.subtranslator.ui.StatusPanel;

/**
 * @author axddd
 *
 */
public class SubTranslatorApp {

	private final SrtPanelController srtPanelController;
	private final MenuItemsController menuItemsController;
	private final MenuBarController menuBarController;

	public SubTranslatorApp() {

		try {
			for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					System.out.println("Nimbus installed");
					UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (Exception e) {
			// If Nimbus is not available, you can set the GUI to another look and feel.
			System.out.println("Nimbus is not installed");
		}

		final EventBus eventBus = new EventBus();

		final JFrame f = new JFrame();

		f.setTitle("Subtitle Translator");
		srtPanelController = new SrtPanelController(eventBus);
		menuBarController = new MenuBarController(srtPanelController, eventBus);
		menuItemsController = new MenuItemsController(srtPanelController, menuBarController, f, eventBus);
		menuItemsController.createView();

		final ISaveListener l = new ISaveListener() {

			@Override
			public void onSave(String absoluteFilePath) {
				srtPanelController.saveTarget(absoluteFilePath);
			}
		};

		menuItemsController.addSaveListener(l);
		f.getContentPane().add( menuBarController.getView(), BorderLayout.NORTH );
		f.getContentPane().add( srtPanelController.getView(), BorderLayout.CENTER );
		int w = Toolkit.getDefaultToolkit().getScreenSize().width;
		int h = Toolkit.getDefaultToolkit().getScreenSize().height;

		f.setSize( 2*w/3, 2*h/3);
		f.setLocation(w/6, h/6);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		f.setIconImage(new ImageIcon(getClass().getResource("asterisk_orange.png")).getImage());

		startGoogleAnaltytics();

		// create the status bar panel and shove it down the bottom of the frame
		StatusBarController statusController = new StatusBarController(eventBus);
		JPanel statusPanel = statusController.getView();
		f.add(statusPanel, BorderLayout.SOUTH);
		statusPanel.setPreferredSize(new Dimension(f.getWidth(), 20));
		
		f.setVisible(true);

		EventQueue queue = Toolkit.getDefaultToolkit().getSystemEventQueue();
		queue.push(new EventQueueProxy(f));

	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater( new Runnable() {
			@Override
			public void run() {
				new SubTranslatorApp();
			}
		});
	}

	private void startGoogleAnaltytics() {
//		try {
//			JGoogleAnalyticsTracker tracker = new JGoogleAnalyticsTracker("subtitle translator","1.3.2","UA-27134249-1");
//			FocusPoint focusPoint = new FocusPoint("AppStart");
//			tracker.trackAsynchronously(focusPoint);	
//		} catch (Exception e) {
//			System.out.println("Stat not available");
//		}
	}

}
